Completed
Push — master ( 605e3d...3de0bb )
by Muhammad Dyas
14s queued 13s
created

state.ts ➔ getStateFromCardWhenNoHeader   C

Complexity

Conditions 9

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 6.6666
c 0
b 0
f 0
cc 9
1
import {ClosableType, PollForm, PollFormInputs, PollState} from './interfaces';
2
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
3
import {MAX_NUM_OF_OPTIONS} from '../config/default';
4
5
/**
6
 * Add a new option to the state(like DB)
7
 *
8
 * @param {string} option - the new option name
9
 * @param {object} state - the current message state
10
 * @param {string} creator - who add the new option
11
 * @returns {void} card
12
 */
13
export function addOptionToState(option: string, state: PollState, creator = '') {
14
  const choiceLength = state.choices.length;
15
  state.choices.push(option);
16
  if (state.choiceCreator === undefined) {
17
    state.choiceCreator = {[choiceLength]: creator};
18
  } else {
19
    state.choiceCreator[choiceLength] = creator;
20
  }
21
}
22
23
export function getStateFromCard(event: chatV1.Schema$DeprecatedEvent) {
24
  const card = event.message?.cardsV2?.[0]?.card as chatV1.Schema$GoogleAppsCardV1Card;
25
  if (!card) {
26
    throw new ReferenceError('no valid card in the event');
27
  }
28
  return getStateFromCardName(card) || getStateFromParameter(event) || getStateFromCardWhenNoHeader(card) ||
29
    getStateFromCardWhenHasHeader(card);
30
}
31
32
function getChoicesFromInput(formValues: PollFormInputs) {
33
  const choices = [];
34
  for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
35
    const choice = formValues[`option${i}`]?.stringInputs!.value![0]!.trim();
36
    if (choice) {
37
      choices.push(choice);
38
    }
39
  }
40
  return choices;
41
}
42
43
function getStringInputValue(input: chatV1.Schema$Inputs) {
44
  if (!input) {
45
    return '';
46
  }
47
  return input.stringInputs!.value![0];
48
}
49
50
export function getConfigFromInput(formValues: PollFormInputs) {
51
  const state: PollForm = {topic: '', choices: []};
52
  state.topic = getStringInputValue(formValues.topic).trim() ?? '';
53
  state.anon = getStringInputValue(formValues.is_anonymous) === '1';
54
  state.optionable = getStringInputValue(formValues.allow_add_option) === '1';
55
  state.type = parseInt(getStringInputValue(formValues.type) || '1') as ClosableType;
56
  state.autoClose = getStringInputValue(formValues.is_autoclose) === '1';
57
  state.autoMention = getStringInputValue(formValues.auto_mention) === '1';
58
  state.closedTime = parseInt(formValues.close_schedule_time?.dateTimeInput!.msSinceEpoch ?? '0');
59
  state.choices = getChoicesFromInput(formValues);
60
  return state;
61
}
62
63
function getStateFromCardWhenNoHeader(card: chatV1.Schema$GoogleAppsCardV1Card) {
64
  return card.sections?.[0].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value;
65
}
66
67
function getStateFromCardWhenHasHeader(card: chatV1.Schema$GoogleAppsCardV1Card) {
68
  // when has header the first section is header
69
  return card.sections?.[1].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value;
70
}
71
72
export function getStateFromCardName(card: chatV1.Schema$GoogleAppsCardV1Card) {
73
  // when has header the first section is header
74
  return card.name;
75
}
76
77
function getStateFromParameter(event: chatV1.Schema$DeprecatedEvent) {
78
  const parameters = event.common?.parameters;
79
80
  return parameters?.['state'];
81
}
82